********************************************************
 ******************************************************
   **  Where Did the Global Elite Go to School?    **
   **  Hierarchy, Harvard, Home, and Hegemony	   **
   **											   **
   **     Ricardo Salas & Keving Young			   **
   **											   **
   ** 	DO FILE: FIGURES AND TABLES 			   **
 ******************************************************
********************************************************

** Setting the origin and destination folders
global computer "C:\Users\57313\Dropbox\ProyectosKevin\Where Did the Global Elite Go to School Project\" 
global source  	"$computer\Writing and Notes\Submission after conditional acceptance - June 2024\datasets"
global figures 	"$computer\Writing and Notes\Submission after conditional acceptance - June 2024\Figures"
global tables 	"$computer\\Writing and Notes\Submission after conditional acceptance - June 2024\Tables"

** Setting the graphic environment in STATA
*ssc desc groups
*ssc install groups
*ssc install inequal7
set scheme white_tableau 
cap graph set ps mag 600
cap graph set ps pagewidth 16
cap graph set ps pageheight 13

**#  Table 1. Descriptive Volumes of our Sample #**
*************************************************************
use "$source/S&Y_EliteUniversities_TEMP.dta", clear
preserve 
*Elites
 duplicates drop ROLE NAME, force
 tab ROLE
restore
preserve
*Degrees
 duplicates drop ROLE UNI NAME, force
 tab ROLE [w=DEGREE]
restore 
preserve 
*Nationalities 
 duplicates drop ROLE NAT, force
 tab ROLE 
restore 
preserve 
*Universities
 duplicates drop ROLE UNI, force
 tab ROLE
restore 	   
	   

**#  Figure 1. Educational Background of the Global Elite #**
*************************************************************
use "$source/S&Y_EliteUniversities.dta", clear
 encode NAME, gen(name)
  collapse (count) total_est=name, by(UNI)
   gsort -total_est
    gen rank = _n
    gen UniversityTop50 =UNI if rank<=50 
	sum total_est
     inequal2 total_est									// Computing Inequality indexes  
	  twoway (scatter total_est   rank if inlist(rank,1,2,4,5,8), msize(1) mcolor("95 143 195") mlcolor(gs10) mlabel(UNI) mlabsize(*.72))			///	
			 (scatter total_est   rank if inlist(rank,3,6,7), 	  msize(1) mcolor("95 143 195") mlcolor(gs10) mlabel(UNI) mlabsize(*.72) mlabpos(2))	///
			 (scatter total_est   rank if inlist(rank,9), 		  msize(1) mcolor("95 143 195") mlcolor(gs10) mlabel(UNI) mlabsize(*.72) mlabpos(4))	///
			 (scatter total_est   rank if rank>9,				  msize(1) mcolor("95 143 195") mlcolor(gs10)), 										  ///
		legend(off) text(-110 -10 "Gini coefficient: .685", size(vsmall)) 										///
		 ytitle("Count of Educational Background", size(*.9)) ylab(0(60)720, labsize(*.8)) yscale(r(0 720)) ///	 
		 xtitle("Educational Institutions Ranked", size(*.9)) xlab(, labsize(*.8)) 
graph export "$figures/Figure1.pdf", as(pdf) replace  	


**#  Figure 2. Distributions of the Universities in the Full Sample  **
** 	 According to Global Prestige Score and the Degrees Granted.    #**
***********************************************************************
use "$source/S&Y_ElitesInstitutions.dta", clear
xtile ntiles=SSCORE, nq(100)
grstyle init
 grstyle set color hcl bluered, n(2):
 grstyle set color hcl bluered, reverse n(3): p#mfill

violinplot ntiles, vertical noline fill(dc(%30)) color("169 49 84") pdf(ll(0) ul(100))  xtitle("Universities in the full Sample", size(medium)) ytitle(Prestige, size(medium)) label(Prestige Score by percentiles) xlab(,nogrid labsize(*1.4)) ylab(,nogrid labsize(*1.4))
graph save vio1.gph, replace
violinplot ntiles [w=no_All], vertical noline fill(dc(%30)) color("174 182 229") pdf(ll(0) ul(100)) xtitle("Degrees granted to Elite members", size(medium)) ytitle("") yscale(lcolor(white)) yla(none)
graph save vio2.gph, replace

graph combine vio1.gph vio2.gph
graph export "$figures/Figure2.pdf", as(pdf) replace  	

**#  Figure 8. Heatmap of Elite Nationality and the Countries Where They Were Educated #**
******************************************************************************************
use "$source/S&Y_EliteUniversities.dta", clear
keep NAME UNI NAT2 COU GRADUATES 
keep if GRADUATES>2

replace NAT2="OTHER COUNTRY"	if (inlist(NAT,"UNITED STATES", "UNITED KINGDOM", "CHINA","CANADA","FRANCE","INDIA","JAPAN")!=1 & inlist(NAT,"GERMANY","RUSSIA","ITALY","BRAZIL")	!=1)
replace COU="OTHER"	if (inlist(COU,"UNITED STATES", "UNITED KINGDOM", "CHINA","CANADA","FRANCE","INDIA","JAPAN")!=1 & inlist(COU,"GERMANY","RUSSIA","ITALY","BRAZIL")	!=1)
replace COU="UK"	if COU=="UNITED KINGDOM"
replace COU="US" 	if COU=="UNITED STATES" 
*ssc install heatplot	
// Aquí puedo organizarlas de acuerdo al número de individuos
tab NAT2 COU, matcell(L1)
 mata: st_matrix("L1", 100*(st_matrix("L1")  :/ rowsum(st_matrix("L1"))))
 mat li L1, format("%3.1f")
 levelsof NAT2, local(nat) 
  matrix rownames L1 = `nat'
 levelsof COU, local(cou)
  matrix colnames L1 = `cou'
  heatplot L1, values(format(%9.1f) size(*.9)) color(hsv bluered, intensity(.7)) aspectratio(1) 	///
   	xtitle("{bf:University Location}", size(small)) 	xlabel(, labsize(*.8) alternate nogrid noticks labgap(0)) xscale(lstyle(none)) ///
	ytitle("{bf:Nationality}", size(small)) legend(off) ylabel( , labsize(*.8) nogrid noticks labgap(0)) yscale(lstyle(none)) 				
graph export "$figures/Figure8.pdf", as(pdf) replace  	

	 
**#  Figure 10. Educational Background of non-US Global Elite #**
****************************************************************
use "$source/S&Y_EliteUniversities.dta", clear
keep NAME AMERICAN UNI
 encode NAME, gen(name)
  keep if AMERICAN==0
  collapse (count) total_est=name, by(UNI)
   gsort -total_est
    gen rank = _n
    gen UniversityTop50 =UNI  if rank<=50 
	sum total_est
     inequal2 total_est									// Computing Inequality indexes  
	  twoway (scatter total_est   rank if inlist(rank,1,2,4,5,8), msize(1) mcolor(cranberry) mlcolor(cranberry) mlabel(UNI) mlabsize(*.72))			///	
			 (scatter total_est   rank if inlist(rank,3,6,7), 	  msize(1) mcolor(cranberry) mlcolor(cranberry) mlabel(UNI) mlabsize(*.72) mlabpos(2))	///
			 (scatter total_est   rank if inlist(rank,9), 		  msize(1) mcolor(cranberry) mlcolor(cranberry) mlabel(UNI) mlabsize(*.72) mlabpos(4))	///
			 (scatter total_est   rank if rank>9,				  msize(1) mcolor(cranberry) mlcolor(cranberry)), 										  ///
		legend(off) text(-110 -10 "Gini coefficient: .508", size(vsmall)) 										///
		 ytitle("Count of Educational Background", size(*.9)) ylab(0(50)250, labsize(*.8)) yscale(r(0 250)) ysize(13)		///	 
		 xtitle("Educational Institutions Ranked", size(*.9)) xlab(, labsize(*.8)) xsize(16)								
graph export "$figures/Figure10.pdf", as(pdf) replace  	


**#  Figure 16. Educational Background of the Global Elite, by Elite Role and US National Status #**
****************************************************************
use "$source/S&Y_EliteUniversities.dta", clear

keep if AMERICAN!=.
gen one=1
collapse (sum) count=one, by(UNI ROLE AMERICAN)
reshape wide count, i(UNI ROLE) j(AMERICAN)
egen count=rowtotal(count1 count0)
rename count1 USNat
rename count0 nonUSNat

bysort ROLE: egen total_degree_et=sum(count)
gen perc_count=count/total_degree_et
replace perc_count=perc_count*100
bysort ROLE: egen rank_et=rank(-perc_count)

gen top3Name = UNI if rank_et<=3
 replace top3Name =subinstr(top3Name , "UNIVERSITY OF", "", 1)
 replace top3Name =subinstr(top3Name , "UNIVERSITY", "", 1)
 replace top3Name =subinstr(top3Name , "LONDON SCHOOL OF ECONOMICS", "LSE", 1)

bysort ROLE: egen rank2_et=rank(-perc_count), unique
sort ROLE rank_et  rank2_et 
gen clock=3
replace clock=2 if rank_et==rank_et[_n+1] 
replace clock=4 if rank_et==rank_et[_n-1]

twoway (scatter perc_count rank2_et, mlabel(top3Name) mcolor("169 46 84") mlabvpos(clock) mlabcolor("169 46 84")), by(ROLE, row(1)) ytitle("% Elite degrees") xlab(,nogrid) xtitle(Rank)
twoway (scatter count rank2_et, mlabel(top3Name) mcolor("169 46 84") mlabvpos(clock) mlabcolor("169 46 84")), by(ROLE, row(1) note("")) ytitle("Elite degrees") xlab(,nogrid) name(all, replace) xsca(r(0 400)) xlab(0 (200) 400) 

cap bysort ROLE: egen rankus_et=rank(-USNat), unique
twoway (scatter USNat rankus_et if USNat!=., mlabel(top3Name) mcolor(blue %90) mlabvpos(clock) mlabcolor("169 46 84")), by(ROLE, row(1) note("")) ytitle("Elite degrees" "US nationals") xlab(,nogrid)  name(USNat, replace) xsca(r(0 400))  xlab(0 (200) 400) 

cap bysort ROLE: egen ranknus_et=rank(-nonUSNat), unique
twoway (scatter nonUSNat ranknus_et, mlabel(top3Name) mcolor(green %90) mlabcolor("169 46 84")), by(ROLE, row(1) note("")) ytitle("Elite degrees" "non US nationals") xlab(,nogrid) xtitle(Rank) name(nonUSNat, replace) xsca(r(0 400))  xlab(0 (200) 400) 
graph combine all USNat nonUSNat, col(1) ysize(*2.5)
graph export "$figures/Figure16.pdf", as(pdf) replace  	


** APPENDICES 
**#  Figures A1-1 to A 1-5 Roles of Organizational Elites		#**
*******************************************************************
use "C:\Users\jssal\Dropbox\ProyectosKevin\Where Did the Global Elite Go to School Project\Compiling Edgelists\Edgelist_Freq&Graph_9August2023.dta", clear
use "C:\Users\57313\Dropbox\ProyectosKevin\Where Did the Global Elite Go to School Project\Compiling Edgelists\Edgelist_Freq&Graph_9August2023.dta", clear
use "$source/S&Y_EliteUniversities.dta", clear 
 encode NAME, gen(name)
 replace ROLE="Billionaires" if ROLE=="Billionaires, 2020"
 levelsof ROLE, local(levels) 
 foreach l of local levels {
  preserve
   keep if ROLE== "`l'" 
  collapse (count) total_est=name, by(UNI)
   gsort -total_est
    gen rank = _n	  
    gen UniversityTop50 =UNI  if rank<=50 
inequal7 total_est									// Computing Inequality indexes 
 local gini = `r(gini)' 						// creating a variable containing the gini
 local gini2 : display %4.3f `gini'
inequal7 total_est if rank<=50 						// Computing Inequality indexes  
 local gini_50 = `r(gini)' 							// creating a variable containing the gini
 local gini2_50 : display %4.3f `gini_50'
sum  total_est
 local yloc = `r(max)' * .9						
sum  rank 
 local xloc = `r(max)' * .7					
grstyle init
 grstyle set color hcl redblue, n(7) opacity(90): p#markfill 
 grstyle set color hcl redblue, n(7) opacity(0): p#markline 
  twoway (scatter total_est  rank if rank<=2, msize(1.2) mlabel(UNI) mlabsize(*.8))		///
  		 (scatter total_est  rank if rank>2), 										  ///   
	ytitle("Count of Educational Background", size(*.8)) ysize(13)	/// 
	xtitle("Educational Institutions Ranked", size(*.8)) xsize(16)	///
	legend(off)										///
	text(`yloc' `xloc' "Gini All:     `gini2'" "Gini Top 50: `gini2_50'", size(*.8) color(gs3))
   graph export "$figures/FigureA1`l'.png", as(png)	replace     
    restore
grstyle clear	
} 

**#  Table A1-1. Educational Background of the Global Elite #**
*************************************************************
use "$source/S&Y_EliteUniversities.dta", clear 
   gen one=1
    collapse (sum) no_=one, by(ROLE UNI)
     replace ROLE=subinstr(ROLE," ","_",.)
      reshape wide no_, i(UNI) j(ROLE) string
       collapse (sum) no_*, by(UNI)	  
       egen no_All=rowtotal(no_*)
	   	egen rank_All			= rank(-no_All)
	    egen rank_Billionaires 	= rank(-no_Billionaires)
	    egen rank_Corporate 	= rank(-no_Corporate)
	    egen rank_Int_Org		= rank(-no_Int_Org)
	    egen rank_National 		= rank(-no_National)
	    egen rank_Third_Sector	= rank(-no_Third_Sector)
order UNI no_All rank_All no_Billionaires rank_Billionaires no_Corporate rank_Corporate no_Int_Org rank_Int_Org no_National rank_National no_Third_Sector rank_Third_Sector
 gsort -no_All -no_Billionaires -no_Corporate -no_Int_Org -no_National -no_Third_Sector
  br if rank_Billionaires<6 | rank_Corporate<6 | rank_Int_Org<6 | rank_Int_Org<6 | rank_National<6 | rank_Third_Sector<6 
collapse no_All-rank_Third_Sector, by(REPLACED_attribute)
sort rank_All

	

**#   Table A4-1 The case for Excluding US Nationals #**
******************************************************************
use "$source/S&Y_EliteUniversities.dta", clear 
 gen US=(NAT=="UNITED STATES")
 groups UNI if NAT!="UNITED STATES", order(h) select(30)	  
  gen one=1
    collapse (sum) no_=one, by(UNI ROLE US)
     replace ROLE=subinstr(ROLE," ","_",.)
	  reshape wide no_, i(UNI ROLE) j(US)
	   rename no_0 no_OT
	   rename no_1 no_US
      reshape wide no_OT no_US, i(UNI) j(ROLE) string
       egen no_OTAll=rowtotal(no_OT*)
	   	egen rank_OTAll					= rank(-no_OTAll)
	    egen rank_OTBillionaires		= rank(-no_OTBillionaires )
	    egen rank_OTCorporate 			= rank(-no_OTCorporate)
	    egen rank_OTInt_Org				= rank(-no_OTInt_Org)
	    egen rank_OTNational 			= rank(-no_OTNational)
	    egen rank_OTThird_Sector		= rank(-no_OTThird_Sector)
       egen no_USAll=rowtotal(no_US*)
	   	egen rank_USAll					= rank(-no_USAll)
	    egen rank_USBillionaires 		= rank(-no_USBillionaires )
	    egen rank_USCorporate 			= rank(-no_USCorporate)
	    egen rank_USInt_Org				= rank(-no_USInt_Org)
	    egen rank_USNational			= rank(-no_USNational)
	    egen rank_USThird_Sector		= rank(-no_USThird_Sector)

	replace no_OTAll = 0 			if no_OTAll==.
	replace no_OTBillionaires = 0 	if no_OTBillionaires ==.
	replace no_OTCorporate= 0 		if no_OTCorporate  ==.
	replace no_OTInt_Org= 0 		if no_OTInt_Org ==.
	replace no_OTNational = 0 		if no_OTNational ==.
	replace no_OTThird_Sector = 0 	if no_OTThird_Sector ==.
	
	replace no_USAll = 0 			if no_USAll==.
	replace no_USBillionaires = 0 	if no_USBillionaires ==.
	replace no_USCorporate = 0 		if no_USCorporate ==.
	replace no_USInt_Org = 0 		if no_USInt_Org ==.
	replace no_USNational = 0 		if no_USNational ==.
	replace no_USThird_Sector = 0 	if no_USThird_Sector ==.
		 
rename UNI Name
order Name /// 
no_OTAll no_OTBillionaires no_OTCorporate no_OTInt_Org no_OTNational no_OTThird_Sector ///
no_USAll no_USBillionaires no_USCorporate no_USInt_Org no_USNational no_USThird_Sector
sort rank_OTAll
egen highest_rankOT = rowmin(rank_OTBillionaires rank_OTCorporate rank_OTInt_Org rank_OTNational rank_OTThird_Sector)
egen highest_rankUS = rowmin(rank_USBillionaires rank_USCorporate rank_USInt_Org rank_USNational rank_USThird_Sector)

keep Name *OT*
*br Name *OT* if rank_OTAll<11 | rank_OTBillionaires<6 | rank_OTCorporate_Elites<6 | rank_OTIO_Leaders<6 | rank_OTNational_Elites <6 | rank_OTThird_Sector_Elites<6 

